package com.otod.sys.emp.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.otod.base.exception.AppException;
import com.otod.base.json.JsonFailure;
import com.otod.base.json.JsonSuccess;
import com.otod.commons.auth.SessionUserHolder;
import com.otod.commons.auth.SessionUserUtil;
import com.otod.commons.entity.Page;
import com.otod.commons.utils.StringUtils;
import com.otod.commons.web.BaseController;
import com.otod.sys.emp.bo.EmpBO;
import com.otod.sys.emp.bo.EmpBusBO;
import com.otod.sys.emp.mapper.EmpMapper;
import com.otod.sys.emp.service.IEmpDetailService;
import com.otod.sys.emp.service.IEmpService;
import com.otod.sys.org.service.IOrgService;
import com.otod.sys.vo.AuapREmpOrgRoleVo;
import com.otod.sys.vo.AuapTEmpDetailVo;
import com.otod.sys.vo.AuapTEmpVo;

/**
 * 员工管理
 * @author jqiu
 */
@Controller
@RequestMapping("sys/emp")
public class EmpController extends BaseController {

	@Autowired
	private IEmpService empService;
	
	@Autowired
	private IOrgService orgService;

	@Autowired
	private IEmpDetailService empDetailService;

	@Autowired
	private EmpMapper empMapper;


	

	
	/**
	 * 查询员工信息
	 * @param request
	 * @return 
	 */
	@RequestMapping(value = "list", method = RequestMethod.GET)
	@ResponseBody
	public Page queryEmpForList(HttpServletRequest request) {
		Map map = super.getParamMapFromRequest(request);
		
		Page page = new Page(request);
		
		page = empService.queryForList(page, map);
		List<AuapTEmpVo> empList = new ArrayList<AuapTEmpVo>();
		
		empList = page.getAaData();
		
		for (AuapTEmpVo auapTEmpVo : empList) {
			String roleNames = "";
			//查询角色
			List<AuapREmpOrgRoleVo> empOrgRoleList = empService.queryForListRoleName(auapTEmpVo.getUuid());
			for (AuapREmpOrgRoleVo auapREmpOrgRoleVo : empOrgRoleList) {
				if (StringUtils.isEmpty(roleNames)) {
					roleNames = auapREmpOrgRoleVo.getRoleName();
				} else {
					roleNames = roleNames + "," + auapREmpOrgRoleVo.getRoleName();
				}
				auapTEmpVo.setRoleNames(roleNames);
			}
			
			
		}
		return page;
	}
	
	
	

	/**
	 * 检查员工编号是否重复
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "checkRepeat", method = RequestMethod.GET)
	@ResponseBody
	public String checkRepeat(HttpServletRequest request)
			throws Exception {
		String empNo = request.getParameter("empNo").toString();
		Long count = empMapper.isRepeatEmpNo(empNo);
		if (count != null && count >= 1) {
			return JsonFailure.factory("员工编号已经使用");
		}
		return JsonSuccess.factory("无重复");
	}

	
	/**
	 * 获取员工信息
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "getEmpInfo", method = RequestMethod.GET)
	@ResponseBody
	public EmpBO getEmpInfo(HttpServletRequest request) throws Exception {
		String empId = request.getParameter("empId");
		AuapTEmpVo auapTEmpVo = empService.getById(empId);
		AuapTEmpDetailVo auapTEmpDetailVo = empDetailService.getById(empId);
//		List<EmpOrgRoleBO> empOrgRoleBOList = empMapper
//				.getEmpOrgRoleBOListByEmpId(empId);
//		empOrgRoleBOList = formatEmpOrgRoleBOList(empOrgRoleBOList);
		EmpBO empBO = new EmpBO();
		empBO.setEmp(auapTEmpVo);
		empBO.setEmpDetail(auapTEmpDetailVo);
//		empBO.setEmpOrgRoleBOList(empOrgRoleBOList);
		empBO.setEmpOrgRoleIds(empService.findByEmpId(empId));
		return empBO;
	}

	
	/**
	 * 保存员工信息
	 * @param empBO
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value ="save", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, String> save(@RequestBody EmpBO empBO) throws Exception {
		Map<String, String> map = new HashMap<String, String>();

		String empId = empBO.getEmp().getUuid();
		String empRolesIds = empBO.getEmpOrgRoleIds();
		if (StringUtils.isEmpty(empId)) {
			// 创建业务层保存员工所需要的BO
			
			// 1、 设置员工基本信息
			AuapTEmpVo auapTEmpVo = new AuapTEmpVo();
			BeanUtils.copyProperties(empBO.getEmp(), auapTEmpVo);
			auapTEmpVo.setPwd("000000");
			auapTEmpVo.setAsyPwd("000000");
			
			
			
			// 2、 设置员工详细信息
//			AuapTEmpDetailVo AuapTEmpDetailVo = new AuapTEmpDetailVo();
//			BeanUtils.copyProperties(empBO.getEmpDetail(), AuapTEmpDetailVo);
//			empBusBO.setAuapTEmpDetail(AuapTEmpDetailVo);
			// 3、 设置员工机构角色关系
//			setEmpOrgRole(empBO, empBusBO);
			// 调用业务层方法添加员工
			empService.addEmployeeBus(auapTEmpVo,empRolesIds,empBO.getEmpDetail());
		} else {
			// 创建业务层保存员工所需要的BO 
			EmpBusBO empBusBO = new EmpBusBO();
			// 1、 设置员工基本信息
			AuapTEmpVo auapTEmpVo = new AuapTEmpVo();
			BeanUtils.copyProperties(empBO.getEmp(), auapTEmpVo);
			//empBusBO.setAuapTEmp(auapTEmpVo);
			// 2、 设置员工详细信息
//			AuapTEmpDetailVo AuapTEmpDetailVo = new AuapTEmpDetailVo();
//			BeanUtils.copyProperties(empBO.getEmpDetail(), AuapTEmpDetailVo);
//			AuapTEmpDetailVo.setEmpId(empId);
//			empBusBO.setAuapTEmpDetail(AuapTEmpDetailVo);
			// 3、 设置员工机构角色关系
//			setEmpOrgRole(empBO, empBusBO);
			// 调用业务层方法添加员工
			empService.editEmployeeBus(auapTEmpVo,empRolesIds,empBO.getEmpDetail());
		}

		return map;
	}
	
	/**
	 * 查询所有员工
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "findAll", method = RequestMethod.POST)
	@ResponseBody
	public List<AuapTEmpVo> findAll() throws Exception {
		List<AuapTEmpVo> empList = empService.findAll();
		return empList;
	}
	
	/**
	 * 查询除自己以外所有员工
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "queryEmp", method = RequestMethod.POST)
	@ResponseBody
	public List<AuapTEmpVo> queryEmp() throws Exception {
		List<AuapTEmpVo> empList = empService.findAll();
		List<AuapTEmpVo> auapEmpList = new ArrayList<AuapTEmpVo>();
		String empNo = SessionUserUtil.getCurrentUser().getUserNo();
		for (AuapTEmpVo auapTEmpVo : empList) {
			if (StringUtils.isNotEmpty(empNo) && !empNo.equals(auapTEmpVo.getEmpNo()) && !"super".equals(auapTEmpVo.getEmpNo())) {
				auapEmpList.add(auapTEmpVo);
			}
		}
		return auapEmpList;
	}
	
	/**
	 * 查询员工信息
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "findByEmpNo", method = RequestMethod.POST)
	@ResponseBody
	public AuapTEmpVo findByEmpNo(String empNo)throws Exception{
		AuapTEmpVo emp = empService.findByEmpNo(empNo);
		return emp;
	}
	/**
	 * 修改或者 添加员工手机号码
	 * @param oldTelphone 旧手机号码
	 * @param newTelphone 新手机号码
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "updateOrAddEmp", method = RequestMethod.GET)
	@ResponseBody
	public String updateOrAddEmp(String oldTelphone,String newTelphone) throws Exception{

		Pattern p = Pattern.compile("^((1[0-9]))\\d{9}$");  
		  
		Matcher m = p.matcher(newTelphone);  
		
		if(m.matches()==false){
			throw new AppException("手机号码有误");
		}
		
		if(StringUtils.isEmpty(oldTelphone)){
			AuapTEmpDetailVo auapTEmpDetailVo = new AuapTEmpDetailVo();
			auapTEmpDetailVo.setEmpId(getEmpId());
			auapTEmpDetailVo.setMobile(newTelphone);
			empService.addEmpTelphone(auapTEmpDetailVo);
		}else{
			empService.updateEmpTelphone(newTelphone,getEmpId());
		}
		
		return JsonSuccess.factory("操作成功");
	}
	/**
	 * 修改密码
	 * @param password
	 * @return
	 */
	@RequestMapping(value = "updatePassword", method = RequestMethod.GET)
	@ResponseBody
	public String updatePassword(HttpServletRequest request, String password){
		String empId = request.getParameter("uuid");
		empService.updateEmpNoPassWord(empId, password);
		return JsonSuccess.factory("修改密码成功");
	}
	
	/**
	 * 重置密码
	 * @param request
	 * @return 
	 */
	@RequestMapping(value = "resetPassword", method = RequestMethod.GET)
	@ResponseBody
	public String resetPassword(HttpServletRequest request) {
		String empId = request.getParameter("uuid");
		String password="000000";
		empService.updateEmpNoPassWord(empId, password);
		return JsonSuccess.factory("重置密码成功  密码为"+password);
	}
	/**
	 * 查询用户的详细信息
	 * @return
	 */
	@RequestMapping(value = "getAuapTempDetail", method = RequestMethod.GET)
	@ResponseBody
	public AuapTEmpDetailVo getAuapTempDetail(){
		AuapTEmpDetailVo auapTEmpDetailVo = null;
		auapTEmpDetailVo=empDetailService.getById(getEmpId());
		if(auapTEmpDetailVo==null){
			return auapTEmpDetailVo = new AuapTEmpDetailVo();
		}
		return auapTEmpDetailVo;
	}
	
	/**
	 * 得到登录用的UserNo 
	 * @return
	 */
	private static String getUserNo(){
		SessionUserHolder sessionUser = SessionUserUtil.getCurrentUser();
		String userNo= sessionUser.getUserNo(); //得到登录用的UserNo 	
		return userNo;
	}
	/**
	 * 得到uuid
	 * @return
	 */
	private  String getEmpId(){
		String empId = empMapper.getByEmpNo(getUserNo()).getUuid();
		return empId;
	}
	
	
	/**
	 * 删除员工及相关信息
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "deleteById", method = RequestMethod.GET)
	@ResponseBody
	public String deleteById(HttpServletRequest request) throws AppException {
		String empId = request.getParameter("uuid");
		empService.deleteByEmpId(empId);
		return JsonSuccess.factory("删除员工成功");
		
	}
	
	
}
